//日期转换
function dateParse(data) {

    var newDate = new Date(data);
    var year = newDate.getFullYear(); //获取当前年
    var month = newDate.getMonth() + 1; //获取当前月
    var day = newDate.getDate(); //获取当前日
    var dateStr = year + '年' + month + '月' + day + '日';
    return dateStr;
}
//延迟加载
$(function () {
    $("#example").dataTable({
        "iDeferLoading":500
    });
});

//根据诊断信息获取手术详情
function getOperationDetailListById(medicalId){
    $.ajax({
        url:"getOperationInfoByMedicalId",
        method:"post",
        data:{
            medicalId:medicalId
        },
        dataType:"json",
        success:function (data) {
            if(data!=null) {
                if (data.length > 0) {
                    // var newData = JSON.parse(data);
                    var newData = data;
                    var point = $("#apply_content");
                    point.children().remove();
                    for (var i = 0; i < newData.length; i++) {
                        var childTr = $("<tr></tr>");
                        var td1 = $("<td>" + newData[i].operationId + "</td>");
                        var td2 = $("<td><a href='javascript:void(0)' onclick='showPatientDetailByOperationId("+newData[i].operationId+","+newData[i].medicalId+")'>" + newData[i].patientName + "</a></td>");
                        var optType = newData[i].operationType;
                        optType = optType == null?"请尽快处理":optType;
                        var td3 = $("<td>" + newData[i].operationType + "</td>");
                        var optName = newData[i].operationName;
                        optName = optName == null?"请尽快处理":optName;
                        var td4 = $("<td>" + newData[i].operationName + "</td>");
                        var td5 = $("<td>" + newData[i].sectionName + "</td>");
                        var td6 = $("<td>" + newData[i].docterName + "</td>");
                        var td7 = $("<td>" + dateParse(newData[i].operationTime) + "</td>");
                        var td8;
                        if (newData[i].isPay == 0) {
                            td8 = $("<td><span id='isPay' value='0' class='custom-badge status-red'>未缴费</span></td>");
                        } else {
                            td8 = $("<td><span id='isPay' value='1' class='custom-badge status-green'>已缴费</span></td>");
                        }
                        var td9;
                        if (newData[i].isHandle == 0) {
                            td9 = $("<td><span id='isHandle' value='0' class='custom-badge status-red'>未处理</span></td>");
                        } else {
                            td9 = $("<td><span id='isHandle' value='1' class='custom-badge status-green'>已处理</span></td>");
                        }
                        var td10;
                        if (newData[i].isAction == 0) {
                            td10 = $("<td><span id='isAction' value='0' class='custom-badge status-red'>未手术</span></td>");
                        } else {
                            td10 = $("<td><span id='isAction' value='1' class='custom-badge status-green'>已手术</span></td>");
                        }
                        var td11 = $("<td class='text-right'><div class='dropdown dropdown-action'><a href='#' class='action-icon dropdown-toggle' data-toggle='dropdown' aria-expanded='false'><i class='fa fa-ellipsis-v'></i></a><div class='dropdown-menu dropdown-menu-right'>" +
                            "<a class='dropdown-item' href='javascript:void(0)' onclick='setOperationInfo("+newData[i].operationId+")'><i class='fa fa-pencil m-r-5'></i> 修改</a>" +
                            "<a class='dropdown-item' href='javascript:void(0)' onclick='deleteOperationInfoByOperationId("+newData[i].operationId+")' data-toggle='modal' data-target='#delete_appointment'><i class='fa fa-trash-o m-r-5'></i> 删除</a>" +
                            "</div></div></td>");
                        childTr.append(td1).append(td2).append(td3).append(td4).append(td5).append(td6).append(td7).append(td8).append(td9).append(td10).append(td11);
                        point.append(childTr);
                    }
                }
                else{
                    var point = $("#apply_content");
                    point.children().remove();
                    point.append("<tr class='odd'><td valign='top' colspan='10' class='dataTables_empty'>记录为空</td></tr>");
                }
            }else{
                var point = $("#apply_content");
                point.children().remove();
                point.append("<tr class='odd'><td valign='top' colspan='10' class='dataTables_empty'>查无数据</td></tr>");
            }
        }
    });
}


//获取患者详细信息
function showPatientDetailByOperationId(operationId,medicalId){
    console.log(222);
    $.ajax({
        url:"showPatientDetailByOperationId",
        method:"post",
        data:{
            operationId:operationId,
            medicalId:medicalId
        },
        dataType:"json",
        success:function (data) {
            if(data!=null){
                $("#topD").removeClass("do_hide");
                $("#topA").addClass("do_hide");
                $("#topB").addClass("do_hide");
                $("[name='detailPatientName']").val(data.patientName);
                $("[name='detailPatientSex']").val(data.patientSex);
                $("[name='detailPatientOccu']").val(data.patientOccu);
                $("[name='detailPatientTel']").val(data.patientTel);
                $("[name='detailPatientRelatives']").val(data.patientRelatives);
                $("[name='detailPatientRelativesTel']").val(data.patientRelativesTel);
                $("[name='detailRegisterTime']").val(dateParse(data.registerTime));
                var historyPoint = $("#historyRecord");
                historyPoint.children().remove();
                if(data.medicalInfos!= null && data.medicalInfos.length > 0){
                    for(var i =0;i < data.medicalInfos.length;i++){
                        if(data.medicalInfos[i].medicalId == medicalId){
                            continue;
                        }
                        var td1 = $("<td>"+data.medicalInfos[i].sectionName+"</td>");
                        var td2 = $("<td>"+data.medicalInfos[i].docterName+"</td>");
                        var td3 = $("<td>"+dateParse(data.medicalInfos[i].medicalTime)+"</td>");
                        var newTr = $("<tr></tr>");
                        newTr.append(td1).append(td2).append(td3);
                        historyPoint.append(newTr);
                    }

                }
                if(historyPoint.children().length == 0){
                    historyPoint.append("<tr><td colspan='3' class='text-center'>无历史记录</td></tr>");
                }
                $("[name='detailSectionName']").val(data.currentMedicalInfo.sectionName);
                $("[name='detailDocterName']").val(data.currentMedicalInfo.docterName);
                $("[name='detailMedicalSymptom']").val(data.currentMedicalInfo.medicalSymptom);
                $("[name='detailMedicalResult']").val(data.currentMedicalInfo.medicalResult);
                $("[name='detailMedicalOpinions']").val(data.currentMedicalInfo.medicalOpinions);
            }
        }
    });

}



//获取手术信息详情
function setOperationInfo(operationId){
    $.ajax({
        url:"getOperationInfoByOperationId",
        method:"post",
        data:{
            operationId:operationId
        },
        dataType:"json",
        success:function(data){
            if(data != null){

                $("[name='patientName']").val(data.patientName);
                $("[name='sectionName']").val(data.sectionName);
                $("#smt").val(data.operationId);
                $("#exit").val(data.medicalId);
                var sectionId = data.sectionId;
                if(sectionId == null){
                    return;
                }
                $.ajax({
                    url:"selectDocterBySectionId",
                    method:"post",
                    data:{
                        sectionId:sectionId
                    },
                    dataType:"json",
                    success:function(docterData){
                        if(docterData!=null){
                            console.log(111);
                            if(docterData.length > 0){
                                var selectPoint = $("[name='docterId']");
                                selectPoint.children().remove();
                                selectPoint.append($("<option value='0'>-- 请选择主刀医生 --</option>"));
                                for(var i=0;i < docterData.length;i++){
                                    var selectItem = $("<option value = '"+ docterData[i].docterId+"'>"+docterData[i].docterName+"</option>");
                                    selectPoint.append(selectItem);
                                }
                            }else{

                                var selectPoint = $("[name='docterId']");
                                selectPoint.children().remove();
                                selectPoint.append($("<option value='-1'>"+data.sectionName+"手术医生已排满</option>"));
                            }
                        }else{

                            var selectPoint = $("[name='docterId']");
                            selectPoint.children().remove();
                            selectPoint.append($("<option value='-1'>"+data.sectionName+"手术医生已排满</option>"));
                        }
                    }
                });
                $("#topA").addClass("do_hide");
                $("#topB").addClass("do_hide");
                $("#topC").removeClass("do_hide");
            }
        }
    });

}





//删除功能
function deleteOperationInfoByOperationId(operationId){
    $.ajax({
        url:"deleteOperationInfoByOperationId",
        method:"post",
        data:{
            operationId:operationId
        },
        dataType:"json",
        success:function (data) {
            if(data!=null){
                data = parseInt(data);
                if(data > 0){
                    // alert();
                    getOperationDetailListById($("#patientId").val());
                }
            }

        }
    });
}

//修改缴费状态
$(document).on('click','#isPay',function(){
    var operationId = $('#isPay').parent().prev().prev().prev().prev().prev().prev().prev().text();
    var isPay = $("#isPay").attr("value");
    $.ajax({
        url:"updateOperationPayByOperationId",
        method:"post",
        data:{
            operationId:operationId,
            isPay:isPay
        },
        success:function (data) {
            if(data != null){
                if(data > 0){
                    getOperationDetailListById($("#patientId").val());
                }
            }
        }
    });
});

//修改手术状态
$(document).on('click','#isAction',function(){
    var operationId = $('#isAction').parent().prev().prev().prev().prev().prev().prev().prev().prev().prev().text();
    var isAction = $('#isAction').attr("value");
    console.log(operationId);

    $.ajax({
        url:"updateOperationActionByOperationId",
        method:"post",
        data:{
            operationId:operationId,
            isAction:isAction
        },
        success:function (data) {
            if(data != null){
                if(data > 0){
                    getOperationDetailListById($("#patientId").val());
                }
            }
        }
    });
});

//修改处理状态
$(document).on('click','#isHandle',function(){
    var operationId = $('#isHandle').parent().prev().prev().prev().prev().prev().prev().prev().prev().text();
    var isHandle = $("#isHandle").attr("value");

    $.ajax({
        url:"updateOperationHandleByOperationId",
        method:"post",
        data:{
            operationId:operationId,
            isHandle:isHandle
        },
        success:function (data) {
            if(data != null){
                if(data > 0){
                    getOperationDetailListById($("#patientId").val());
                }
            }
        }
    });
});

//查询按钮
$(document).on('click', '#queryBtn',function(){
    getOperationDetailListById($("#patientId").val());
});

//提交按钮
$(document).on('click','#smt',function(){
    console.log("aaa")
    if($("[name='operationType']").val() == 0 || $("[name='operationName']").val() == ""
        || $("[name='operationName']").val() == null || $("[name='docterId']").val() == 0
        || $("[name='operationTime']").val() == "" || $("[name='operationTime']").val() == null){
        $("#msg").text("请完善信息");
        $("#msg").css({
            "color":"red"
        });
        $("#msg").removeClass("do_hide");
        setTimeout(function(){
            $("#msg").addClass("do_hide");
        },1500);
        return;
    }
    if($("[name='docterId']").val() == -1){
        $("#msg").text("暂无可以手术的医生");
        $("#msg").css({
            "color":"red"
        });
        $("#msg").removeClass("do_hide");
        setTimeout(function(){
            $("#msg").addClass("do_hide");
        },1500);
        return;
    }
    var operationType = $("[name='operationType']").val();
    var operationName = $("[name='operationName']").val();
    var docterId = $("[name='docterId']").val();
    var operationTime =$("[name='operationTime']").val();
    var arr = operationTime.split("/");
    var tem=arr[0];
    arr[0] = arr[1];
    arr[1] = tem;
    operationTime=arr.join("/");
    var operationId = $("#smt").val();
    var medicalId = $("#patientId").val();
    $.ajax({
        url:"updateOperationInfoByOperationId",
        type:"post",
        data:{
            "medicalId":medicalId,
            "operationId":operationId,
            "operationType":operationType,
            "operationName":operationName,
            "docterId":docterId,
            "operationTime":operationTime
        },
        dataType:"json",
        success:function (data) {
            if(data != null){
                if(data > 0){
                    $("#topA").removeClass("do_hide");
                    $("#topB").removeClass("do_hide");
                    $("#topC").addClass("do_hide");
                    $("[name='operationType']").val();
                    $("[name='docterId']").val();
                    $("[name='operationName']").val("");

                    $("[name='operationTime']").val("");
                    getOperationDetailListById($("#exit").val());
                }else{
                    $("#msg").text("设置失败");
                    $("#msg").css({
                        "color":"red"
                    });
                    $("#msg").removeClass("do_hide");
                    setTimeout(function(){
                        $("#msg").addClass("do_hide");
                    },1500);
                }
            }else{
                $("#msg").text("提交失败");
                $("#msg").css({
                    "color":"red"
                });
                $("#msg").removeClass("do_hide");
                setTimeout(function(){
                    $("#msg").addClass("do_hide");
                },1500);
            }
        }
    });
});

$(document).on('click','#exit',function(){
    $("#topA").removeClass("do_hide");
    $("#topB").removeClass("do_hide");
    $("#topC").addClass("do_hide");
    $("[name='operationType']").val();
    $("[name='docterId']").val();
    $("[name='operationName']").val("");
    $("[name='operationTime']").val("");
    getOperationDetailListById($("#exit").val());
});


$(document).on("click","#detailExit",function(){
    $("#topD").addClass("do_hide");
    $("#topA").removeClass("do_hide");
    $("#topB").removeClass("do_hide");
    $("[name='detailPatientName']").val("");
    $("[name='detailPatientSex']").val("");
    $("[name='detailPatientOccu']").val("");
    $("[name='detailPatientTel']").val("");
    $("[name='detailPatientRelatives']").val("");
    $("[name='detailPatientRelativesTel']").val("");
    $("[name='detailRegisterTime']").val("");
    var historyPoint = $("#historyRecord");
    historyPoint.children().remove();
    $("[name='detailSectionName']").val("");
    $("[name='detailDocterName']").val("");
    $("[name='detailMedicalSymptom']").val("");
    $("[name='detailMedicalResult']").val("");
    $("[name='detailMedicalOpinions']").val("");
});



